package com.hy.config;

import org.dozer.DozerBeanMapper;
import org.dozer.spring.DozerBeanMapperFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class DozerConfig {

    @Bean(name = "dozerBeanMapper")
    public DozerBeanMapper dozerBeanMapperFactoryBean() throws Exception {
        DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            dozerBeanMapperFactoryBean.setMappingFiles(resolver.getResources("classpath:dozer/*.xml"));
        }catch (Exception e){
            List<String> mappingFiles = new ArrayList<>();
            DozerBeanMapper dozerBean = new DozerBeanMapper();
            dozerBean.setMappingFiles(mappingFiles);
            return dozerBean;
        }
        return (DozerBeanMapper)dozerBeanMapperFactoryBean.getObject();
    }
}
